In [1]:
import numpy as np 
import pandas as pd 
import skimage.color as color
import os
total_files=[]

fp="/wheat/train/"
for dirname, _, filenames in os.walk(fp):
    total_files=filenames
In [2]:
root_path = "wheat/"
train_folder = os.path.join(root_path, "train/")
test_folder = os.path.join(root_path, "test/")
train_csv_path = os.path.join(root_path, "train.csv")
sample_submission = os.path.join(root_path, "sample_submission.csv")
In [3]:
train_df=pd.read_csv(train_csv_path)
In [16]:
from skimage.filters import laplace
import numpy as np
import skimage.filters as filter
import matplotlib.pyplot as plt
from skimage.filters import laplace
from skimage import io
fig,ax=plt.subplots(1,2,figsize=(24,24))
for en in range(2):    
    image = io.imread(train_folder +str(train_df.loc[0,"image_id"])+".jpg")
    if en==0:
        ax[en].imshow(image)
    
        continue

    im = color.rgb2gray((image))
    im1 = np.clip(laplace(im) + im, 0,1 )
    ax[en].imshow(im1)
In [5]:
import skimage.segmentation as seg
import skimage.filters as filters
import skimage.draw as draw
import skimage.color as color
import skimage.transform as tfm

def image_show(image, nrows=1, ncols=1,cmap="gray"):
    fig, ax = plt.subplots(nrows=nrows, ncols=ncols, figsize=(8, 8))
    ax.imshow(image,cmap="gray")
    ax.axis('off')
    return fig, ax
fig,ax=plt.subplots(1,1)
ax.hist(image.ravel(),bins=64,range=[0,300])
ax.set_xlim(0,300)
Out[5]:
(0, 300)
In [6]:
fig,ax=plt.subplots(1,5,figsize=(12,12))
for en,i in enumerate([0,50,60,80,100]):
            
    image = io.imread(train_folder+str(train_df.loc[0,"image_id"])+".jpg")
    if en==0:
        
        ax[en].imshow(tfm.resize(image,(512,512)))
        continue
   
    mask=image<i
    image[mask]=0
    
    gimage=color.rgb2gray(image)
    im = color.rgb2gray((image))
    im1 = np.clip(laplace(im) + im, 0,1 )
    ax[en].imshow(gimage,cmap="gray")
#image_show(fimage)
C:\Users\HP\Untitled Folder 1\lib\site-packages\skimage\transform\_warps.py:105: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
  warn("The default mode, 'constant', will be changed to 'reflect' in "
C:\Users\HP\Untitled Folder 1\lib\site-packages\skimage\transform\_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images.
  warn("Anti-aliasing will be enabled by default in skimage 0.15 to "
In [7]:
t_filter=[filters.threshold_li,
filters.threshold_triangle,
          filters.threshold_yen,
          filters.threshold_isodata,
          filters.threshold_sauvola
         ]
fig,ax=plt.subplots(1,5,figsize=(32,32))
fig.tight_layout(pad=3.0)
for en,i in enumerate(t_filter):
    tit=str(i)       
    image = io.imread(train_folder+str(train_df.loc[0,"image_id"])+".jpg")
   
    i=i(image)
    mask=image<i
    image[mask]=0
    
    gimage=(color.rgb2gray(image))
    ax[en].imshow(gimage,cmap="gray")
    ax[en].set_title(tit,fontsize=20)
In [8]:
t_filter=[
filters.threshold_mean,
          filters.threshold_minimum,
          filters.threshold_niblack,
          filters.threshold_yen
         ]

fig,ax=plt.subplots(1,4,figsize=(22,22))
fig.tight_layout(pad=3.0)
for en,i in enumerate(t_filter):
    tit=str(i)       
    image = io.imread(train_folder+str(train_df.loc[0,"image_id"])+".jpg")
   
    
    i=i(image)
    mask=image<i
    image[mask]=0
    
    gimage=(color.rgb2gray(image))
    ax[en].imshow(gimage,cmap="gray")
    ax[en].set_title(tit)
In [9]:
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

from skimage import data
from skimage.filters import threshold_yen
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import closing, square,rectangle,convex_hull_object
from skimage.color import label2rgb

def make_rect(name):
    
    n_image = io.imread(test_folder+str(name))

    image = color.rgb2gray(n_image)


    thresh = filters.threshold_yen(image)
    
    bw=closing(image < thresh, square(4))
    
    

    
    label_image = label(bw,connectivity=1,background=255)

    image_label_overlay = label2rgb(label_image, image=image, bg_label=0)

    


    fig, ax = plt.subplots(figsize=(10, 6))
    regionprops(label_image)
    ax.imshow(n_image)

    listrect=[]
    for region in regionprops(label_image):
        # take regions with large enough areas
        if region.area >= 500:
            # draw rectangle around segmented coins
            minr, minc, maxr, maxc = region.bbox
            l=(maxc-minc)*(maxr-minr)*0.0264583333
            listrect.append([int((l//10)*0.0264583333)])
            
            rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                                      fill=False, edgecolor='red', linewidth=2)
            ax.add_patch(rect)

    
    return listrect
    
In [10]:
d=dict()
fp=test_folder
for dirname, _, filenames in os.walk(fp):
    total_files=filenames
data_list=list(set(train_df["image_id"].values))
for k in range(len(data_list)):
    data_list[k]=data_list[k]+".jpg"
notthere=[]
for i in filenames:
    if i not in data_list:
        notthere.append(i)
print(len(notthere))
10
In [17]:
temp_df=pd.DataFrame({"image_id":[],"bbox":[]})

for i in notthere:
    rects=make_rect(i)
    rects=rects
    rects=" ".join(str(item) for innerlist in rects for item in innerlist)
    d={"image_id":[],"bbox":[]}
    
    
    d["bbox"].append(rects)
    d["image_id"].append(i[:-4])
    
       # print(pd.DataFrame(d))  
    temp_df=temp_df.append(pd.DataFrame(d),ignore_index=True)
    
In [12]:
temp_df["bbox"]
Out[12]:
0    73 2 0 1 0 1 2 0 0 0 0 0 0 0 0 5 0 0 0 0 1 0 0...
1    73 1 0 0 0 0 6 0 0 0 0 0 1 0 0 2 0 14 0 0 0 0 ...
2    73 60 0 0 5 0 0 0 0 0 0 0 0 1 2 0 4 0 0 0 0 0 ...
3    73 0 6 0 12 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 ...
4    3 61 0 57 2 0 0 1 0 8 0 0 1 7 0 0 0 0 0 0 0 0 ...
5    0 73 0 2 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 1 7 0...
6    73 2 0 9 0 5 1 0 0 3 0 0 0 0 0 0 0 0 0 0 0 14 ...
7    0 4 0 0 11 0 73 5 0 0 0 0 1 0 0 0 0 0 0 0 3 0 ...
8    2 73 0 2 0 2 0 0 36 0 14 0 0 0 0 0 0 0 0 4 1 0...
9    73 0 2 4 0 8 0 0 0 0 1 0 0 0 0 7 0 0 1 0 0 1 0...
Name: bbox, dtype: object